package com.think.common.enums.serialize;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.think.common.cons.SysCons;
import com.think.common.enums.IEnum;
import com.think.common.enums.SEnum;

import java.io.IOException;

/**
 * <p>
 * jackson SEnum 序列化器
 * </p>
 * @author CaiWenYi
 * @Date: 2019/6/19 16:18
*/
public class SEnumSerialize extends StdSerializer<SEnum> {

    public SEnumSerialize(Class<SEnum> i) {
        super(i);
    }

    @Override
    public void serialize(SEnum sEnum, JsonGenerator gen, SerializerProvider provider) throws IOException {
        String code = sEnum.getValue();
        gen.writeNumber(code);
        gen.writeStringField(gen.getOutputContext().getCurrentName() + SysCons.ENUM_SERIALIZE_POSTFIX,
                sEnum.getMsg());
    }
}
